package com.example.lib_common.base

import android.graphics.Color
import android.os.Bundle
import android.view.*
import androidx.annotation.LayoutRes
import androidx.core.view.WindowCompat
import androidx.fragment.app.DialogFragment
import com.example.lib_common.R
import androidx.core.graphics.drawable.toDrawable

class EdgePopDialog() : DialogFragment() {

    enum class Anim {
        TOP, BOTTOM, LEFT, RIGHT, CENTER
    }

    companion object {
        private const val LAYOUT_RES = "layout_res"
        private const val ARG_ANIM = "arg_anim"
        private const val ARG_CANCEL_OUTSIDE = "arg_cancel_outside"
        private const val ARG_DIM = "arg_dim"

        private const val WIDTH_LAYOUT_PARAM = "width_Layout_param"

        private const val HEIGHT_LAYOUT_PARAM = "height_Layout_param"
        fun newInstance(
            @LayoutRes layoutResId: Int,
            anim: Anim,
            cancelOnTouchOutside: Boolean = true,
            dimAmount: Float = 0.45f,
            widthLayoutParam: Int = ViewGroup.LayoutParams.MATCH_PARENT,
            heightLayoutParam: Int = ViewGroup.LayoutParams.WRAP_CONTENT,
            listener: (View) -> Unit = {}
        ): EdgePopDialog = EdgePopDialog().apply {
            viewListener = listener
            arguments = Bundle().apply {
                putInt(LAYOUT_RES, layoutResId)
                putInt(WIDTH_LAYOUT_PARAM, widthLayoutParam)
                putInt(HEIGHT_LAYOUT_PARAM, heightLayoutParam)
                putString(ARG_ANIM, anim.name)
                putBoolean(ARG_CANCEL_OUTSIDE, cancelOnTouchOutside)
                putFloat(ARG_DIM, dimAmount)
            }
        }
    }

    private lateinit var viewListener: (View) -> Unit


    private val anim: Anim by lazy {
        Anim.valueOf(requireArguments().getString(ARG_ANIM)!!)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.EdgeDialogTheme)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        val contentView = inflater.inflate(requireArguments().getInt(LAYOUT_RES), container, false)
        viewListener.invoke(contentView)
        return contentView
    }


    override fun onStart() {
        super.onStart()
        dialog?.window?.let { w ->
            // 全屏覆盖状态栏和导航栏
            w.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
            w.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            w.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            w.setFlags(
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            )
            w.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)


            w.attributes = w.attributes.apply {
                dimAmount = requireArguments().getFloat(ARG_DIM, 0.45f)
                windowAnimations = when (anim) {
                    Anim.TOP -> R.style.EdgeDialog_Anim_Top
                    Anim.BOTTOM -> R.style.EdgeDialog_Anim_Bottom
                    Anim.LEFT -> R.style.EdgeDialog_Anim_Left
                    Anim.RIGHT -> R.style.EdgeDialog_Anim_Right
//                    Anim.CENTER -> R.style.EdgeDialog_Anim_Center
                    Anim.CENTER -> 0
                }
                gravity = when (anim) {
                    Anim.TOP -> Gravity.TOP
                    Anim.BOTTOM -> Gravity.BOTTOM
                    Anim.LEFT -> Gravity.START
                    Anim.RIGHT -> Gravity.END
                    Anim.CENTER -> Gravity.CENTER
                }
            }

            w.setLayout(
                requireArguments().getInt(WIDTH_LAYOUT_PARAM),
                requireArguments().getInt(HEIGHT_LAYOUT_PARAM)
            )

            // 点击外部是否可取消
            dialog?.setCanceledOnTouchOutside(
                requireArguments().getBoolean(
                    ARG_CANCEL_OUTSIDE, true
                )
            )

            // WindowInsets：沉浸 + 不遮状态栏
            WindowCompat.setDecorFitsSystemWindows(w, false)
        }
    }
}